document.observe("dom:loaded", function() {
    // alert('test');
    // define the key elements
    var section_template = $('form-section_');
    var question_template = $('form-question_');

    /* listeners for:
      * all .remove-question button
        * #add-question 
        * all .add-possible-answer
        * all .remove-possible-answer
        * #add-section
        * all .remove-section
    */

    // renumber possible answers
    function reindex_possible_answers(parentTd) {
        var possibles = parentTd.down('div.possible-answer');
        return possibles.size();
    }

    // function to add a possible answer div by clicking a member add-possible-answer button
    function add_possible_answer(btn) {
        var container = btn.up('div.possible-answer');
        var newContainer = Element.clone(container, true);
        var parentTd = container.up('td');
        parentTd.appendChild(newContainer);

        // listen to child add button
        var childAdd = newContainer.down('.add-possible-answer');
        childAdd.observe("click", function(e) {
            var cbtn = Event.element(e);
            add_possible_answer(cbtn);
        });

        // listen to child remove button
        var childRemove = newContainer.down('.remove-possible-answer');
        childRemove.observe('click', function(e) {
            var cbtn = Event.element(e);
            remove_possible_answer(cbtn);
        });
    }

    // remove the possible answer div by clicking on child remove-possible-answer button
    function remove_possible_answer(btn) {
        var container = btn.up('div.possible-answer');
        container.remove();
    }

    // listen to all add-possible-answer buttons
    $$(".add-possible-answer").invoke("observe", "click", function(e) {
        // alert('add possible answer');
        var btn = Event.element(e);
        add_possible_answer(btn);

        // listen to the child container's add and remove buttons
    });

    // function to add a question from template
    function add_question(question_element) {
        var newQuestion = Element.clone(question_element, true);
        var parentSection = question_element.up('.form-section');
        parentSection.appendChild(newQuestion);
    }

    // listener for request to add new question 
    $$(".add-question").invoke("observe", "click", function(e) {
        var btn = Event.element(e);
        var container = btn.up("div.form-question");
        var newQuestion = Element.clone(container, true);
        var parentSection = btn.up("div.form-section");
        container.insert({'after': newQuestion});
    });

    // function to add a section from template
    function add_section(btn) {
        var section = btn.up('div.form-section');
        var newSection = Element.clone(section, true);
        document.body.appendChild(newSection);
    }

    $$(".add-section").invoke("observe", "click", function(e) {
        var btn = Event.element(e);
        // alert('asdf');
        var container = btn.up("div.form-section");
        var newSection = Element.clone(container, true);
        (document.body).appendChild(newSection);
    });
});
